package com.my.content.liveproject;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.my.content.search.SearchActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 这个碎片是主要页面内容的碎片，和MineFragment相同之处是搜索还有消息的activity的跳转
 * 主要是顶部的tablayout和viewpager关联然后拿到数据后显示出界面
 */
public class MainFragment extends Fragment implements View.OnClickListener {

    @BindView(R.id.main_fragment_search)
    ImageView mainFragmentSearch;
    @BindView(R.id.main_fragment_more)
    ImageView mainFragmentMore;
    @BindView(R.id.main_fragment_tab)
    TabLayout mainFragmentTab;
    @BindView(R.id.main_viewPager)
    ViewPager mainViewPager;

    public MainFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        String[]titles = getResources().getStringArray(R.array.tabTitle);
        List<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            Fragment fragment = FragmentFactory.createFragment(i);
            fragments.add(fragment);
        }

//        view.findViewById(R.id.main_fragment_search).setOnClickListener(this);//搜索图标控件
//        view.findViewById(R.id.main_fragment_more).setOnClickListener(this);//信息图标控件
        MainLvAdapter adapter = new MainLvAdapter(getActivity().getSupportFragmentManager(),fragments,titles);
        mainViewPager.setAdapter(adapter);

        mainFragmentTab.setupWithViewPager(mainViewPager);
    }


    @OnClick({R.id.main_fragment_search, R.id.main_fragment_more})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_fragment_search:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
            case R.id.main_fragment_more:
                break;
        }
    }
}
